//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
//  File: io.c
//
//  This file implements Mainstone II input-output routines.
//
#include <windows.h>


UINT16 READ_PORT_USHORT(UINT16 *pAddr)
{
    UINT32 nAlignedAddr = ((UINT32)pAddr & ~0x3);

    if ((UINT32)pAddr & 0x2)    // Is the address an "odd" word within a longword?
    {
        // Yes - read a longword and data is in the upper word.
        return((UINT16) ((*(volatile unsigned long *)nAlignedAddr) >> 16));
    }
    else
    {
        // No - read single word.
        return(*(volatile unsigned short *)nAlignedAddr);
    }
}


void WRITE_PORT_USHORT(UINT16 *pAddr, UINT16 Data)
{
    UINT32 nAlignedAddr = ((UINT32)pAddr & ~0x3);

    if ((UINT32)pAddr & 0x2)    // Is the address an "odd" word within a longword?
    {
        // Yes - write a longword with data in the upper word.
        *(volatile UINT32 *)nAlignedAddr = (READ_PORT_USHORT(pAddr) | (Data << 16));
    }
    else
    {
        // No - write single word.
        *(volatile UINT16 *)pAddr = Data;
    }
}



UCHAR
READ_PORT_UCHAR(
    PUCHAR  Port
    )
{
    return *(volatile UCHAR * const)Port;
}

VOID
WRITE_PORT_UCHAR(
    PUCHAR  Port,
    UCHAR   Value
    )
{
    *(volatile UCHAR * const)Port = Value;
}

VOID
WRITE_REGISTER_USHORT(
    PUSHORT Register,
    USHORT  Value
    )
{
    *(volatile USHORT * const)Register = Value;
}


VOID
WRITE_REGISTER_ULONG(
    PULONG  Register,
    ULONG   Value
    )
{
    *(volatile ULONG * const)Register = Value;
}

USHORT
READ_REGISTER_USHORT(
    PUSHORT Register
    )
{
    return (*(volatile USHORT * const)Register);
}


ULONG
READ_REGISTER_ULONG(
    PULONG  Register
    )
{
    return (*(volatile ULONG * const)Register);
}
